<?php
/*
	收入明细
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'bufee >'=>0);
	if($_RQ['type']=='islast'){
		$params['lastbufee >']=0;
		$return['params']['type']=$_RQ['type'];
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if($_RQ['mobile']){
		$params['mobile like']='%'.trim($_RQ['mobile']).'%';
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$total='';
	$list=pdo_getslice('jy_yxw_member',$params,array($pindex,$psize),$total,$fields,'','id asc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['id']);
			$kechengid=pdo_getcolumn('jy_yxw_class',array('id'=>$item['classid'],'uniacid'=>$_SESSION['uniacid']),'kechengid');
			$item['kechengtitle']=pdo_getcolumn('jy_yxw_kecheng',array('id'=>$kechengid,'uniacid'=>$_SESSION['uniacid']),'title');
			$item['isnum']=pdo_count('jy_yxw_bufee_log',array('memberid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			if($item['lastbufee >']==0 && $_RQ['type']=='islast'){
				$item['isadd']=1;
			}
		}
	}
	$heji=array('member'=>array('nickname'=>'合计'));
	foreach($list as $k=>&$v){
		$heji['bufee']=$v['bufee'];
		$heji['lastbufee'] +=$v['lastbufee'];
		$heji['isnum'] +=$v['isnum'];
	}
	$list[]=$heji;
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]：这里处理各门店的收费情况','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'title','title'=>'姓名','type'=>'string'),
			array('name'=>'cardno','title'=>'身证号','type'=>'string'),
			array('name'=>'mobile','title'=>'手机号','type'=>'string'),
			array('name'=>'bankno1','title'=>'卡号','type'=>'string'),
			array('name'=>'kechengtitle','title'=>'产品','type'=>'string'),
			array('name'=>'classid','title'=>'售后群','type'=>'select2','options'=>'class'),
			array('type'=>'radio2','title'=>'状态','name'=>'status','options'=>'status'),
			array('name'=>'bufee','title'=>'补贴总额','type'=>'string'),
			array('name'=>'lastbufee','title'=>'补贴余额','type'=>'string'),
			array('name'=>'isnum','title'=>'已发期数','type'=>'string'),
		),'action'=>array(
			array('type'=>'href','action'=>'jy_yxw/role14.bufee_member/check/','title'=>'补贴发放','show'=>array('isadd'=>1)),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'用户姓名','type'=>'string'),
			array('type'=>'string','width'=>'10','title'=>'手机号','name'=>'mobile','help'=>''),
			array('name'=>'storeid','width'=>15,'title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'createtime','width'=>15,'title'=>'时间段','type'=>'time_range')
		)
	);
	$return['status']=$this->member_status;
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['workers']=$this->getall('worker',array('role'=>14,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['class']=$this->getall('class',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['htmls']=$htmls;
	$return['pay_types']=$this->pay_types;
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['fee'] || $post['fee']<=0){
			exi('请先输入【本期应发金额】!','error');
		}
		if($post['fee']>$post['lastbufee']){
			exi('【本期应发金额】不能大于【补贴余额】!','error');
		}
		$id=$post['id'];
		$sn=$post['isnum']+1;
		$data=array(
			'fee'=>$post['fee'],
			'bankno1'=>$post['bankno1'],
			'bankno2'=>$post['bankno2'],
			'remark'=>trim($post['remark']),
			'status'=>intval($post['status']),
			'sn'=>'第'.$sn.'期'
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['memberid']=$id;
		$data['storeid']=$post['storeid'];
		$data['up_time']=time();
		$data['createtime']=time();
		$data['up_workerid']=$_SESSION['workerid'];
		$this->insert('bufee_log',$data);
		if($post['status']==1){
			pdo_update('jy_yxw_member',array('lastbufee -='=>$post['fee']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}
		exi('操作成功！','','/role14.bufee_member//status=6');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['storetitle']=pdo_getcolumn('jy_yxw_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['storeid']),'title');
		$item['classtitle']=pdo_getcolumn('jy_yxw_class',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['classid']),'title');
		$item['classtitle']=$item['classtitle']?:'无';
		$item['statustitle']=$this->member_status[$item['status']];
		$item['isnum']=pdo_count('jy_yxw_bufee_log',array('memberid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		$item['bankno']=$item['bankno1'];
	}else{
		exi('请先选择记录','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
	);
	$htmls['edit']=array(
		array('name'=>'id','title'=>'ID','type'=>'copy'),
		array('name'=>'storetitle','title'=>'所属门店','type'=>'copy'),
		array('name'=>'title','title'=>'姓名','type'=>'copy'),
		array('name'=>'cardno','title'=>'身证号','type'=>'copy'),
		array('name'=>'mobile','title'=>'手机号','type'=>'copy'),
		array('name'=>'bankno','title'=>'卡号','type'=>'copy'),
		array('name'=>'kechengtitle','title'=>'产品','type'=>'copy'),
		array('name'=>'classtitle','title'=>'售后群','type'=>'copy'),
		array('name'=>'statustitle','title'=>'状态','type'=>'copy'),
		array('name'=>'bufee','title'=>'补贴总额','type'=>'copy'),
		array('name'=>'lastbufee','title'=>'补贴余额','type'=>'copy'),
		array('name'=>'isnum','title'=>'已发期数','type'=>'copy'),
		array('name'=>'fee','title'=>'本期应发金额','type'=>'string'),
		array('name'=>'bankno1','title'=>'一账户','type'=>'string'),
		array('name'=>'bankno2','title'=>'二账户','type'=>'string'),
		array('type'=>'radio','title'=>'发放状态','name'=>'status','options'=>'status'),
		array('type'=>'text','title'=>'发放备注','name'=>'remark'),
	);
	$return['status']=array('待发放','已发放','发放异常');
	$return['htmls']=$htmls;
	exi($return);
}
